package com.aibili.contoller.api;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aibili.pojo.FeedBack;
import com.aibili.pojo.HelpInfo;
import com.aibili.pojo.LvInfo;
import com.aibili.pojo.NoticeInfo;
import com.aibili.pojo.UserDevice;
import com.aibili.pojo.UserInfo;
import com.aibili.pojo.UserLocation;
import com.aibili.pojo.VideoEvaluate;
import com.aibili.service.FileService;
import com.aibili.service.NoticeInfoService;
import com.aibili.service.UserDeviceService;
import com.aibili.service.UserFavorabilityService;
import com.aibili.service.UserInfoService;
import com.aibili.service.UserLocationService;
import com.alibaba.fastjson.JSON;
import com.daoshun.common.CommonUtils;
import com.daoshun.common.DsConstant;
import com.daoshun.exception.CustomException;

@Controller
@RequestMapping("/api/common")
public class ApiCommonController extends ApiBaseContoller {

	@Resource
	private NoticeInfoService noticeInfoService;

	@Resource
	private UserInfoService userInfoService;

	@Resource
	private UserFavorabilityService userFavorabilityService;

	@Resource
	private FileService fileService;

	@Resource
	private UserLocationService userLocationService;

	@Resource
	private UserDeviceService userDeviceService;

	/**
	 * 
	 * @Description 系统通知
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/notice", produces = "application/json; charset=UTF-8")
	public String notice(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		try {
			request.setCharacterEncoding("utf-8");
			String user_id = request.getParameter("user_id");
			List<NoticeInfo> infos = noticeInfoService.getByUserid(CommonUtils.parseLong(user_id, 0));
			map.put("list", infos);
			map.putAll(DsConstant.SUCCESS_RESULT_MAP);
			return toJson(map);
		} catch (Exception e) {
			e.printStackTrace();
			return DsConstant.EXCEPTION;
		}
	}

	/**
	 * 
	 * @Description 文件上传
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/file", produces = "application/json; charset=UTF-8")
	public String file(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		try {
			request.setCharacterEncoding("utf-8");
			long file_id = getFileId(request);
			map.put("file_id", file_id);
			map.putAll(DsConstant.SUCCESS_RESULT_MAP);
			return JSON.toJSONString(map);
		} catch (Exception e) {
			e.printStackTrace();
			return DsConstant.EXCEPTION;
		}
	}

	/**
	 * 
	 * @Description 增加经验值
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/exp", produces = "application/json; charset=UTF-8")
	public String addExp(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		try {
			request.setCharacterEncoding("utf-8");
			String user_id = request.getParameter("user_id");
			String exp = request.getParameter("exp");
			CommonUtils.validateEmpty(user_id);
			CommonUtils.validateEmpty(exp);

			UserInfo user = userInfoService.getById(CommonUtils.parseLong(user_id, 0));
			if (user != null) {
				userInfoService.addExp(user, 0, CommonUtils.parseInt(exp, 0));
				map.putAll(DsConstant.SUCCESS_RESULT_MAP);
				return JSON.toJSONString(map);
			} else {
				return DsConstant.EXCEPTION;
			}

		} catch (CustomException e) {
			return e.getMessage();
		} catch (Exception e) {
			e.printStackTrace();
			return DsConstant.EXCEPTION;
		}
	}

	/**
	 * 
	 * @Description 增加好感度
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/favorability", produces = "application/json; charset=UTF-8")
	public String addFavorability(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		try {
			request.setCharacterEncoding("utf-8");
			String from_id = request.getParameter("from_id");
			String to_id = request.getParameter("to_id");
			String favorability = request.getParameter("favorability");
			CommonUtils.validateEmpty(from_id);
			CommonUtils.validateEmpty(to_id);
			CommonUtils.validateEmpty(favorability);

			userFavorabilityService.addFavorability(CommonUtils.parseLong(from_id, 0), CommonUtils.parseLong(to_id, 0),
					0, CommonUtils.parseInt(favorability, 0));
			map.putAll(DsConstant.SUCCESS_RESULT_MAP);
			return JSON.toJSONString(map);
		} catch (CustomException e) {
			return e.getMessage();
		} catch (Exception e) {
			e.printStackTrace();
			return DsConstant.EXCEPTION;
		}
	}

	/**
	 * 
	 * @Description 上传位置信息
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/uploadlocation", produces = "application/json; charset=UTF-8")
	public String uploadlocation(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		try {
			request.setCharacterEncoding("utf-8");
			String user_id = request.getParameter("user_id");
			String longitude = request.getParameter("longitude");
			String latitude = request.getParameter("latitude");
			CommonUtils.validateEmpty(user_id);
			CommonUtils.validateEmpty(longitude);
			CommonUtils.validateEmpty(latitude);

			UserLocation location = userLocationService.getByUserid(CommonUtils.parseLong(user_id, 0));
			if (location != null) {
				location.setLatitude(CommonUtils.parseInt(latitude, 0));
				location.setLongitude(CommonUtils.parseInt(longitude, 0));

				userLocationService.update(location);
			} else {
				location = new UserLocation();
				location.setLatitude(CommonUtils.parseInt(latitude, 0));
				location.setLongitude(CommonUtils.parseInt(longitude, 0));
				location.setUser_id(CommonUtils.parseLong(user_id, 0));

				userLocationService.add(location);
			}

			map.putAll(DsConstant.SUCCESS_RESULT_MAP);
			return JSON.toJSONString(map);
		} catch (CustomException e) {
			return e.getMessage();
		} catch (Exception e) {
			e.printStackTrace();
			return DsConstant.EXCEPTION;
		}
	}

	/**
	 * 
	 * @Description 上传设备信息
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/uploaddevice", produces = "application/json; charset=UTF-8")
	public String uploaddevice(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		try {
			request.setCharacterEncoding("utf-8");
			String user_id = request.getParameter("user_id");
			String client_id = request.getParameter("client_id");
			String type = request.getParameter("type");
			CommonUtils.validateEmpty(user_id);
			CommonUtils.validateEmpty(client_id);
			CommonUtils.validateEmpty(type);

			int ty = CommonUtils.parseInt(type, 0);
			long uid = CommonUtils.parseInt(user_id, 0);
			UserDevice device = userDeviceService.getByClientid(client_id, ty);
			if (device != null) {
				device.setUser_id(uid);

				userDeviceService.update(device);
			} else {
				device = new UserDevice();
				device.setUser_id(uid);
				device.setClient_id(client_id);
				device.setType(ty);

				userDeviceService.add(device);
			}

			map.putAll(DsConstant.SUCCESS_RESULT_MAP);
			return JSON.toJSONString(map);
		} catch (CustomException e) {
			return e.getMessage();
		} catch (Exception e) {
			e.printStackTrace();
			return DsConstant.EXCEPTION;
		}
	}

	/**
	 * 
	 * @Description 用户等级表
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/lvinfo", produces = "application/json; charset=UTF-8")
	public String lvinfo(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		try {
			request.setCharacterEncoding("utf-8");
			List<LvInfo> list = userInfoService.getLvinfo();
			map.put("lv_list", list);
			map.putAll(DsConstant.SUCCESS_RESULT_MAP);
			return toJson(map);
		} catch (Exception e) {
			e.printStackTrace();
			return DsConstant.EXCEPTION;
		}
	}
	
	/**
	 * 视频评级
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/video_evaluate", produces = "application/json; charset=UTF-8")
	public String videoevaluate(HttpServletRequest request, HttpServletResponse response){
		HashMap<String, Object> map = new HashMap<String, Object>();
		try {
			request.setCharacterEncoding("utf-8");
			String user_id = request.getParameter("user_id");
			String rate_id = request.getParameter("rate_id");
			String grade = request.getParameter("grade");
			String content = request.getParameter("content");//评价内容
		
			CommonUtils.validateEmpty(user_id);
			CommonUtils.validateEmpty(rate_id);
			CommonUtils.validateEmpty(grade);
			CommonUtils.validateEmpty(content);
			
			long userid = CommonUtils.parseLong(user_id, 0);
			long rateid = CommonUtils.parseLong(rate_id, 0);
			int cograde = CommonUtils.parseInt(grade, 0);
			
			VideoEvaluate evaluate = new VideoEvaluate();
			evaluate.setUser_id(userid);
			evaluate.setRate_id(rateid);
			evaluate.setGrade(cograde);
			evaluate.setContent(content);
			evaluate.setCreate_time(new Date());
			userInfoService.saveVideoEvaluate(evaluate);
			int avg = userInfoService.getAVG(rateid);
			UserInfo user = userInfoService.getById(rateid);
			user.setVideo_evaluate(avg);
			userInfoService.updateUserInfo(user);
			map.putAll(DsConstant.SUCCESS_RESULT_MAP);
			return JSON.toJSONString(map);
		} catch (CustomException e) {
			return e.getMessage();
		} catch (Exception e) {
			e.printStackTrace();
			return DsConstant.EXCEPTION;
		}
	}
	

	/**
	 * 用户反馈
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/feedback", produces = "application/json; charset=UTF-8")
	public String feedback(HttpServletRequest request, HttpServletResponse response){
		HashMap<String, Object> map = new HashMap<String, Object>();
		try {
			request.setCharacterEncoding("utf-8");
			String user_id = request.getParameter("user_id");
			String content = request.getParameter("content");//评价内容
			CommonUtils.validateEmpty(user_id);
			CommonUtils.validateEmpty(content);
			long userid = CommonUtils.parseLong(user_id, 0);
			FeedBack feed = new FeedBack();
			feed.setUser_id(userid);
			feed.setContent(content);
			feed.setCreate_time(new Date());
			userInfoService.saveFeedBack(feed);
			map.putAll(DsConstant.SUCCESS_RESULT_MAP);
			return JSON.toJSONString(map);
		} catch (CustomException e) {
			return e.getMessage();
		} catch (Exception e) {
			e.printStackTrace();
			return DsConstant.EXCEPTION;
		}
	}
	
	/**
	 * 帮助信息
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/helpinfo", produces = "application/json; charset=UTF-8")
	public String helpinfo(HttpServletRequest request, HttpServletResponse response){
		HashMap<String, Object> map = new HashMap<String, Object>();
		try {
			request.setCharacterEncoding("utf-8");
			List<HelpInfo> list = userInfoService.getHelpInfo();
			map.put("helpinfo_list", list);
			map.putAll(DsConstant.SUCCESS_RESULT_MAP);
			return toJson(map);
		} catch (Exception e) {
			e.printStackTrace();
			return DsConstant.EXCEPTION;
		}
	}
	
	@ResponseBody
	@RequestMapping(value = "/video_list", produces = "application/json; charset=UTF-8")
	public String video_list(HttpServletRequest request, HttpServletResponse response){
		HashMap<String, Object> map = new HashMap<String, Object>();
		try {
			request.setCharacterEncoding("utf-8");
			String user_id = request.getParameter("user_id");
			CommonUtils.validateEmpty(user_id);
			long userid = CommonUtils.parseLong(user_id, 0);
			String page = request.getParameter("page");
			CommonUtils.validateEmpty(page);
			int Page = CommonUtils.parseInt(page, 0);
			List<VideoEvaluate> list = userInfoService.getVideoEvaluatelist(userid,Page);
			long amount = list.size();
			map.put("commentList", list);
			map.put("amount", amount);
			map.putAll(DsConstant.SUCCESS_RESULT_MAP);
			return toJson(map);
		} catch (Exception e) {
			e.printStackTrace();
			return DsConstant.EXCEPTION;
		}
	}
	
	@ResponseBody
	@RequestMapping(value = "/get_mine", produces = "application/json; charset=UTF-8")
	public String get_mine(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		try {
			request.setCharacterEncoding("utf-8");
			String user_id = request.getParameter("user_id");
			String mobile = request.getParameter("mobile");
			if (!CommonUtils.isEmptyString(user_id)) {
				long userid = CommonUtils.parseLong(user_id, 0);
				UserInfo info = userInfoService.getById(userid);
				if (info != null) {
					map.put("user", info);
					map.putAll(DsConstant.SUCCESS_RESULT_MAP);
					return toJson(map);
				} else {
					return DsConstant.NO_USER;
				}
				
			}else{
				UserInfo user = userInfoService.getUserByPhone(mobile);
				if (user != null) {
					map.put("user", user);
					map.putAll(DsConstant.SUCCESS_RESULT_MAP);
					return toJson(map);
				}else{
					return DsConstant.NO_USER;
					
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			return DsConstant.EXCEPTION;
		}

	}
	
}
